package com.service.zk;

import com.alibaba.fastjson2.JSON;
import com.bean.zk.Emp;
import com.dao.zk.EmpDAO;
import com.dao.zk.HousehodeDAO;
import com.dao.zk.RoleDAO;
import org.springframework.stereotype.Service;
import wiremock.net.minidev.json.JSONArray;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpService {
    @Resource
    private EmpDAO empDAO;
    @Resource
    private HousehodeDAO househodeDAO;
    @Resource
    private RoleDAO  roleDAO;


    public boolean getIdByNameAndPswd(String account, String pswd, String userType, HttpServletRequest req) {
        //  查询emp
        Map<String,Object>  maps =null;
        if (userType.equals("emp")) {
            maps =empDAO.select(account,pswd);
        } else if (userType.equals("household")) {
            maps =househodeDAO.select(account,pswd);
        }
        if (maps !=null) {
//            将用户存入session中
            HttpSession session = req.getSession();
            session.setAttribute("user", maps);
            return true;
        } else {
            return false;
        }
    }

    public List<Map<String, Object>> getEmps() {
        //  查询emps
        return empDAO.getEmps();
    }

    public List<Map<String, Object>> getEmpByBlur(String ename, String did) {
        return empDAO.getEmpBlur(ename, Integer.parseInt(did));
    }

    public int insertemp(Emp emp) {
        return empDAO.insert(emp);
    }

    public int delete (String eid){
        List<Map<String, Object>> maps = roleDAO.geRolesByEid(Integer.parseInt(eid));
        if (maps.size() != 0) {
            //该用户拥有角色，不能删除
            return -1;
        }else {
            return empDAO.delete(Integer.parseInt(eid));
        }
    }

    public Map<String ,Object> select (String eid){
        return empDAO.getEmpByEid(Integer.parseInt(eid));
    }

    public int update(String eid ,Emp emp){
        return empDAO.update(Integer.parseInt(eid) ,emp);
    }


    /**
     * 根据号码查询
     * @param emp
     * @return
     */
    public Map<String, Object> selectByPhone(Emp emp) {
        return empDAO.selectByPhone(emp);

    }

    /**
     * 修改密码
     * @param emp
     * @return 返回影响行数
     */
    public int updatePswd(Emp emp) {

            return empDAO.updatePswd(emp);
    }
}
